package com.yf.dxapp.module.app.service.appbanner;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import com.yf.dxapp.module.app.controller.admin.appbanner.vo.AppBannerPageReqVO;
import com.yf.dxapp.module.app.controller.admin.appbanner.vo.AppBannerSaveReqVO;
import com.yf.dxapp.module.app.dal.dataobject.appbanner.AppBannerDO;
import com.yf.dxapp.module.app.dal.mysql.appbanner.AppBannerMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.yf.dxapp.module.app.enums.ErrorCodeConstants.DATA_NOT_EXISTS;

/**
 * 广告图 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class AppBannerServiceImpl implements AppBannerService {

    @Resource
    private AppBannerMapper appBannerMapper;

    @Override
    public Long createAppBanner(AppBannerSaveReqVO createReqVO) {
        // 插入
        AppBannerDO appBanner = BeanUtils.toBean(createReqVO, AppBannerDO.class);
        appBannerMapper.insert(appBanner);
        // 返回
        return appBanner.getId();
    }

    @Override
    public void updateAppBanner(AppBannerSaveReqVO updateReqVO) {
        // 校验存在
        validateAppBannerExists(updateReqVO.getId());
        // 更新
        AppBannerDO updateObj = BeanUtils.toBean(updateReqVO, AppBannerDO.class);
        appBannerMapper.updateById(updateObj);
    }

    @Override
    public void deleteAppBanner(Long id) {
        // 校验存在
        validateAppBannerExists(id);
        // 删除
        appBannerMapper.deleteById(id);
    }

    private void validateAppBannerExists(Long id) {
        if (appBannerMapper.selectById(id) == null) {
            throw exception(DATA_NOT_EXISTS);
        }
    }

    @Override
    public AppBannerDO getAppBanner(Long id) {
        return appBannerMapper.selectById(id);
    }

    @Override
    public PageResult<AppBannerDO> getAppBannerPage(AppBannerPageReqVO pageReqVO) {
        return appBannerMapper.selectPage(pageReqVO);
    }

    @Override
    public List<AppBannerDO> findByType(Integer type) {
        return appBannerMapper.selectList(AppBannerDO::getType,type);
    }

}